
// RayTracerView.cpp : implementation of the CRayTracerView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "RayTracer.h"
#endif

#include "RayTracerDoc.h"
#include "RayTracerView.h"

#include "Basis/Image.h"
#include "Render/Renderer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CRayTracerView

IMPLEMENT_DYNCREATE(CRayTracerView, CView)

BEGIN_MESSAGE_MAP(CRayTracerView, CView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CRayTracerView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_SIZE()
END_MESSAGE_MAP()

// CRayTracerView construction/destruction

CRayTracerView::CRayTracerView()
{
	// TODO: add construction code here
	m_renderFlag = false;
	m_pRenderer = NULL;
}

CRayTracerView::~CRayTracerView()
{
	if (m_pRenderer)
		delete m_pRenderer;
	m_pRenderer = NULL;
}

BOOL CRayTracerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);

}

// CRayTracerView drawing

void CRayTracerView::OnDraw(CDC* pDC/*pDC*/)
{
	CRayTracerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here

	if (m_renderFlag)
	{
		Image* pImage = m_pRenderer->getImage();
		float scale = 255.0f;
		int height = pImage->getHeight();
		int width = pImage->getWidth();
		rgb color;
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				pImage->get(i, j, color, scale);
				pDC->SetPixel(i, height - 1 - j, RGB(color.r(), color.g(), color.b()));
			}
		}
	}
}


// CRayTracerView printing


void CRayTracerView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CRayTracerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CRayTracerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CRayTracerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CRayTracerView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CRayTracerView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CRayTracerView diagnostics

#ifdef _DEBUG
void CRayTracerView::AssertValid() const
{
	CView::AssertValid();
}

void CRayTracerView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CRayTracerDoc* CRayTracerView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRayTracerDoc)));
	return (CRayTracerDoc*)m_pDocument;
}
#endif //_DEBUG


// CRayTracerView message handlers


void CRayTracerView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
}

bool CRayTracerView::getRenderFlag()
{
	return m_renderFlag;
}

void CRayTracerView::setRenderFlag(bool renderFlag)
{
	m_renderFlag = renderFlag;
}

Renderer* CRayTracerView::getRenderer()
{
	return m_pRenderer;
}

void CRayTracerView::OnInitialUpdate()
{
	CView::OnInitialUpdate();

	// TODO: Add your specialized code here and/or call the base class
	if (!m_pRenderer)
	{
		CRect rect;
		GetClientRect(&rect);
		m_pRenderer = new Renderer();
		m_pRenderer->init(rect.Width(), rect.Height());
	}
}
